/*
################################################################################
#
# Replication file for: Ingram, Matthew C. 2016. "Mandates, Geography, and 
#     Networks: Diffusion of Criminal Procedure Reform in Mexico."
#     Latin American Politics and Society 58: 121-145.
#     DOI: http://dx.doi.org/10.1111/j.1548-2456.2016.00301.x
# Author: Matthew C. Ingram
# Affiliation: University at Albany, SUNY
# contact: mingram@albany.edu
# NOTE: this file complements other command files (see replication materials)
# Date last reviewed: 20180216 (for submission to journal repository)
####################################################################
Note: 
this is code for R&R for LAPS, mainly from MAY 2015, for article accepted in 
Fall 2015;
code covers the following items from publication:
(1) Figure 2
(2) Table 1
(3) Table 2
Analysis focuses solely on aggregate CCP index (variable name: add2ccp)
See article for description.

See separate command files in R for data management, 
generation of random graphs, and other figures
from publication.
################################################################################
*/

* SET WORKING DIRECTORY

* change directory to folder where data is stored; in my case, this is:
cd "C:\Users\mi122167\Dropbox\Criminal Procedure\pubs\Ingram2016LAPS\replicationmats"

* load data
* merged data set in 2015 was loaded as follows: 
use "data\crimprodata_20180216_small.dta", clear

/*
* note: this data includes all data (base data and data generated 
from random graphs);
see two separate R scripts for details on generation of random graphs 
and management of associated data
*/

* Figure 2 

tsset code year
tsline add2ccp if year>2001 & year<2012, by(state) ///
	xtitle("Year") ytitle("Reform Index") ///
	graphregion(color(white)) plotregion(color(white))


* set data from time-series cross-sectional analysis (xt family of commands)
xtset code year

* do base GEE model with multiple alternative measures of competition
* e.g., enc, enc_molinar, margin_gov, md_gov
* stfedopp dropped because collinear with pan_gov (corr = -0.92)

* Table 2, model 6
xtgee f1.add2ccp enc pan_gov prd_gov fedreform lpopden lngdpcap if year>2000
est store m1

* Table 1, summary statistics, generated from sample included in above model
sum f1.add2ccp enc enc_molinar margin_gov md_gov ///
	pan_gov prd_gov pri_gov ///
	fedreform yr2004 ///
	lngdpcap lpopden ///
	i4lag i4plag if e(sample)
 
* robustness checks with different measures of competition
xtgee f1.add2ccp enc_molinar pan_gov prd_gov fedreform lpopden lngdpcap ///
	if year>2000
est store m2
xtgee f1.add2ccp margin_gov pan_gov prd_gov fedreform lpopden lngdpcap ///
	if year>2000
est store m3
xtgee f1.add2ccp md_gov pan_gov prd_gov fedreform lpopden lngdpcap if year>2000
est store m4


* check robustness if exclude DF 

xtgee f1.add2ccp enc pan_gov prd_gov fedreform lpopden lngdpcap ///
	if year>2000 & code!=9
est store m1b
xtgee f1.add2ccp enc_molinar pan_gov prd_gov fedreform lpopden lngdpcap ///
	if year>2000 & code!=9
est store m2b
xtgee f1.add2ccp margin_gov pan_gov prd_gov fedreform lpopden lngdpcap ///
	if year>2000 & code!=9
est store m3b
xtgee f1.add2ccp md_gov pan_gov prd_gov fedreform lpopden lngdpcap ///
	if year>2000 & code!=9
est store m4b

* do base model above, with split sample before 2008 and after 2008
* need to drop stfedopp due to collinearity with pan_gov
xtgee f1.add2ccp enc pan_gov prd_gov stfedopp lpopden lngdpcap if year>2000 ///
	& year<2008
est store m5
xtgee f1.add2ccp enc pan_gov prd_gov stfedopp lpopden lngdpcap if year>2007
est store m6


* do base model above, with variable for pre/post-2004
gen yr2004=.
replace yr2004=0 if year<2004
replace yr2004=1 if year>2003

* Table 2, model 4
xtgee f1.add2ccp enc pan_gov prd_gov fedreform yr2004 lpopden lngdpcap ///
	if year>2000
est store m7
* robustness check
xtgee f1.add2ccp enc pan_gov prd_gov fedreform yr2004 lpopden lngdpcap ///
	if year>2000 & code!=9
est store m8

* Table 2, model 5
xtgee f1.add2ccp enc pan_gov prd_gov  yr2004 lpopden lngdpcap ///
	if year>2000 & year<2008
est store m9
* robustness check
xtgee f1.add2ccp enc pan_gov prd_gov  yr2004 lpopden lngdpcap ///
	if year>2000 & year<2008 & code!=9
est store m10


* ##############################################################################
* ADDING SPATIAL LAGS, RANDOM GRAPH LAGS, and PARTY NETWORK LAGS
* ##############################################################################

* SPATIAL LAGS
tsset code year
* Table 2, model 2
xtgee f1.add2ccp enc pan_gov prd_gov fedreform yr2004 i4lag lpopden lngdpcap ///
	if year>2000
est store m14

* outreg2 [m14] using table5, e(all) word excel replace

* PARTY NETWORK LAGS
* Table 2, model 3
xtgee f1.add2ccp enc pan_gov prd_gov fedreform yr2004 i4plag lpopden ///
	lngdpcap if year>2000
est store m19

* robustness check; network lag without party dummies
xtgee f1.add2ccp enc fedreform yr2004 i4plag lpopden lngdpcap if year>2000
est store m19a

* outreg2 [m16 m17 m18 m19 m20] using table6, e(all) word excel replace

* both Spatial Lag and Party Lag
* Table 2, model 1
xtgee f1.add2ccp enc pan_gov prd_gov fedreform yr2004 i4lag i4plag lpopden ///
	lngdpcap if year>2000
est store m24

* robustness check; both lags, but no party dummies
xtgee f1.add2ccp enc fedreform yr2004 i4lag i4plag lpopden lngdpcap if year>2000
est store m24a

* TABLE 2
* from above, export models for Table 2
* note: the model numbers within parentheses identify the models above that 
* were reported in Table 2
outreg2 [m24 m14 m19 m7 m9 m1] using table2, e(all) word excel replace


* ROBUSTNESS WITH RANDOM GRAPHS LAGS 
* (as discussed on pp. 135-136 in published article)

* W1 
tsset code year
xtgee f1.add1 enc pan_gov prd_gov fedreform yr2004 w1i1 lpopden lngdpcap ///
	if year>2000
est store m111
xtgee f1.add1leg enc pan_gov prd_gov fedreform yr2004 w1i2 lpopden ///
	lngdpcap if year>2000
est store m112
xtgee f1.add1imp enc pan_gov prd_gov fedreform yr2004 w1i3 lpopden ///
	lngdpcap if year>2000
est store m113
xtgee f1.add2ccp enc pan_gov prd_gov fedreform yr2004 w1i4 lpopden ///
	lngdpcap if year>2000
est store m114
xtgee f1.add2les enc pan_gov prd_gov fedreform yr2004 w1i5 lpopden ///
	lngdpcap if year>2000
est store m115


* W2
tsset code year
xtgee f1.add1 enc pan_gov prd_gov fedreform yr2004 w2i1 lpopden lngdpcap ///
	if year>2000
est store m211
xtgee f1.add1leg enc pan_gov prd_gov fedreform yr2004 w2i2 lpopden ///
	lngdpcap if year>2000
est store m212
xtgee f1.add1imp enc pan_gov prd_gov fedreform yr2004 w2i3 lpopden ///
	lngdpcap if year>2000
est store m213
xtgee f1.add2ccp enc pan_gov prd_gov fedreform yr2004 w2i4 lpopden ///
	lngdpcap if year>2000
est store m214
xtgee f1.add2les enc pan_gov prd_gov fedreform yr2004 w2i5 lpopden ///
	lngdpcap if year>2000
est store m215

/*
NOTES:
all random graph lags are statistically significant EXCEPT for add2ccp
inference: results regarding party and spatial adjacency matrices are not 
discernible from results obtained 
from randomly generated adjacency matrix EXCEPT in the case of 
add2ccp (CCP index);
this is main rationale for focusing solely on CCP index in LAPS R&R
*/

* end
